'use strict'

require('make-promises-safe')

const fs = require('fs')
const path = require('path')

let mydebug = require('./mydebug');
let controller = require('./controllers/controller');

// const fastify = require('fastify')({
//     http2: true,
//     https: {
//         allowHTTP1: true,
//         key: fs.readFileSync(path.join(__dirname, 'cert', 'key.pem')),
//         cert: fs.readFileSync(path.join(__dirname, 'cert', 'cert.pem'))
//     }
// })
const fastify = require('fastify')()

fastify.get('/', async (req, rsp) => {
    return { hello: 'test' }
})

fastify.get('/status', async (req, rsp) => {
    return controller.getStatus();
})

const opts = {
    schema: {
        body: {
            type: 'object',
            properties: {
                wpb: {type: 'number'}
            }
        }
    }
}
fastify.post('/cmd', opts, async (req, rsp) => {
    controller.sendCmd(req.body);
    return req.body;
})

const start = async () => {
    try {
        await fastify.listen(30000, '0.0.0.0')
        mydebug.LogOutput('fastify start successfully.');
    } catch (err) {
        fastify.log.error(err)
        console.log('fastify start failed.')
        process.exit(1)
    }
}

start()

